
import Page from '../../common/page';
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';

//获取应用实例
const app = getApp()

Page({
  data: {
    visible: false,
    userInfo: {},
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    options: [
      {
        name: '名称',
        icon: 'https://img.yzcdn.cn/vant/custom-icon-fire.png',
      },
      {
        name: '名称',
        icon: 'https://img.yzcdn.cn/vant/custom-icon-light.png',
      },
      {
        name: '名称',
        icon: 'https://img.yzcdn.cn/vant/custom-icon-water.png',
      },
    ],
  },
  onLoad: function () {
    this.getUserInfo()
    this.testToast()
  },
  tabClick() {
    Notify({
      message: '别急，功能还没开发！',
      type: 'warning'
      // color: '#fff',
      // background: '#1badae',
    });    
  },
  testToast() {
    const toast = Toast.loading({
      duration: 0, // 持续展示 toast
      forbidClick: true,
      message: '倒计时 3 秒'
    });
    let second = 3;
    const timer = setInterval(() => {
      second--;
      if (second) {
        toast.setData({
          message: `倒计时 ${second} 秒`,
        });
      } else {
        clearInterval(timer);
        Toast.clear();
      }
    }, 1000);
  },
  getUserInfo() {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true,
        tapDialogVisible: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true,
          tapDialogVisible: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  onClick(event) {
    this.setData({ visible: true });
  },

  onClose() {
    this.setData({ visible: false });
  },
})
